package com.guiji.spider.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guiji.common.annotation.Log;
import com.guiji.common.core.controller.BaseController;
import com.guiji.common.core.domain.AjaxResult;
import com.guiji.common.enums.BusinessType;
import com.guiji.common.utils.poi.ExcelUtil;
import com.guiji.spider.domain.SpiderConfig;
import com.guiji.spider.service.ISpiderConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 爬虫配置Controller
 *
 * @author wujiyue
 * @date 2019-11-11
 */
@Controller
@RequestMapping("/spider/spiderConfig")
@Api(value = "爬虫配置", tags = {"爬虫配置"})
public class SpiderConfigController extends BaseController {
	private String prefix = "com/guiji/spider/spiderConfig";

	@Autowired
	private ISpiderConfigService spiderConfigService;

	//    @RequiresPermissions("spider:spiderConfig:view")
	@GetMapping()
	@ApiOperation("spiderConfig")
	public String spiderConfig() {
		return prefix + "/spiderConfig";
	}

	/**
	 * 查询爬虫配置列表
	 */
//    @RequiresPermissions("spider:spiderConfig:list")
	@PostMapping("/list")
	@ResponseBody
	@ApiOperation("查询爬虫配置列表")
	public PageInfo list(SpiderConfig spiderConfig) {
		PageHelper.startPage(spiderConfig.getPageNum(),spiderConfig.getPageSize());
		List<SpiderConfig> list = spiderConfigService.selectSpiderConfigList(spiderConfig);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

	/**
	 * 导出爬虫配置列表
	 */
//    @RequiresPermissions("spider:spiderConfig:export")
	@PostMapping("/export")
	@ResponseBody
	@ApiOperation("导出爬虫配置列表")
	public AjaxResult export(SpiderConfig spiderConfig) {
		List<SpiderConfig> list = spiderConfigService.selectSpiderConfigList(spiderConfig);
		ExcelUtil<SpiderConfig> util = new ExcelUtil<SpiderConfig>(SpiderConfig.class);
		return util.exportExcel(list, "spiderConfig");
	}

	/**
	 * 新增爬虫配置
	 */
	@GetMapping("/add")
	@ApiOperation("新增爬虫配置")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存爬虫配置
	 */
//    @RequiresPermissions("spider:spiderConfig:add")
	@Log(title = "爬虫配置", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	@ApiOperation("新增保存爬虫配置")
	public AjaxResult addSave(SpiderConfig spiderConfig) {
		SpiderConfig check = spiderConfigService.selectSpiderConfigByCode(spiderConfig.getSpiderCode());
		if (check != null) {
			return AjaxResult.error("爬虫编码[" + spiderConfig.getSpiderCode() + "]已经存在!");
		}
		return toAjax(spiderConfigService.insertSpiderConfig(spiderConfig));
	}

	/**
	 * 修改爬虫配置
	 */
	@GetMapping("/edit/{id}")
	@ApiOperation("修改爬虫配置")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		SpiderConfig spiderConfig = spiderConfigService.selectSpiderConfigById(id);
		mmap.put("spiderConfig", spiderConfig);
		return prefix + "/edit";
	}

	/**
	 * 修改保存爬虫配置
	 */
//    @RequiresPermissions("spider:spiderConfig:edit")
	@Log(title = "爬虫配置", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	@ApiOperation("修改保存爬虫配置")
	public AjaxResult editSave(SpiderConfig spiderConfig) {
		return toAjax(spiderConfigService.updateSpiderConfig(spiderConfig));
	}

	/**
	 * 删除爬虫配置
	 */
//    @RequiresPermissions("spider:spiderConfig:remove")
	@Log(title = "爬虫配置", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	@ApiOperation("删除爬虫配置")
	public AjaxResult remove(String ids) {
		return toAjax(spiderConfigService.deleteSpiderConfigByIds(ids));
	}

}
